import datetime
from coin.exchange.base.kr_rest.native_public_client_base import (
    NativePublicClientBase)


class OkexNativePublicClient(NativePublicClientBase):
  def __init__(self, timeout=10):
    super().__init__(url="https://www.okex.com", timeout=timeout)
    self._valid_kline_period = [
       '1m', '3m', '5m', '15m', '30m', '1H', '2H', '4H', '6H', '12H', '1D', '1W', '1M', '1Y'
    ]

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, \
      'Invalid kline period!'

  def get_exchange_ticker(self):
    path = 'api/v5/market/tickers'
    params = {'instType': 'SPOT'}
    update = self._get_as_json(path, params=params)
    return update

  def get_ticker(self, instrument_id):
    path = 'api/v5/market/ticker'
    params = {'instId': instrument_id}
    update = self._get_as_json(path, params=params)
    return update

  def get_history_kline(self, instrument_id, kline_period, start=None, end=None):
    self.check_kline_period(kline_period)
    path = 'api/v5/market/history-candles'
    params = {
        'instId': instrument_id,
        'bar': kline_period
    }
    if start:
      params['before'] = int(start.timestamp() * 1000)
    if end:
      params['after'] = int(end.timestamp() * 1000)
    return self._get_as_json(path, params)

  def queryAll(self):
    pass
